package cn.tedu.egweibo3.controller;

import cn.tedu.egweibo3.conmon.response.JsonResult;
import cn.tedu.egweibo3.conmon.response.StatusCode;
import cn.tedu.egweibo3.mapper.WeiboMapper;
import cn.tedu.egweibo3.pojo.dto.WeiboDTO;
import cn.tedu.egweibo3.pojo.dto.WeiboDTO1;
import cn.tedu.egweibo3.pojo.entity.Weibo;
import cn.tedu.egweibo3.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibos/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发布微博功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody @Validated WeiboDTO weiboDTO,@ApiIgnore HttpSession session){
        log.debug("weiboDTO:"+weiboDTO);
        UserVO userVO=(UserVO)session.getAttribute("user");
        Weibo weibo = new Weibo();
        weibo.setContent(weiboDTO.getContent());
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        log.debug("weibo:"+weibo);
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }

    @ApiOperation("微博首页列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
        return JsonResult.ok(weiboMapper.selectIndex());
    }

    @ApiOperation("微博详情页")
    @GetMapping("selectById")
    public JsonResult selectById( WeiboDTO1 weiboDTO1){
        log.debug("weiboDTO1:"+weiboDTO1);
        Long id = weiboDTO1.getId();
        String username = weiboDTO1.getUsername();
        return JsonResult.ok(weiboMapper.selectWeiboDetail(id,username));
    }


}
